﻿Imports System.Threading

Public Class Helper
    Dim MainJSON As Dictionary(Of String, Object)

    Sub New(ByVal Dic As Dictionary(Of String, Object))
        MainJSON = Dic
    End Sub

#Region ">> Functions for DOJSON"

    Function GetNameByL(ByVal L As Integer) As String
        For Each item In MainJSON.Item("data").Item("ret").Item("itemInfo")
            If item.Item("L") = L Then
                Return item.Item("name")
            End If
        Next
        Return ""
    End Function

    Function GetNameByResourceID(ByVal I As Integer) As String
        For Each useritem In MainJSON.Item("data").Item("ret").Item("items")
            If useritem.Item("I") = I Then
                For Each item In MainJSON.Item("data").Item("ret").Item("itemInfo")
                    If item.Item("L") = useritem.Item("L") Then
                        Return item.Item("name")
                    End If
                Next
                Return ""
            End If
        Next
        Return ""
    End Function

    Function GetTypeByT(ByVal T As Integer) As String
        Dim cnt As Integer = 0
        For Each item In MainJSON.Item("data").Item("map").Item("types")
            cnt = cnt + 1
        Next
        For i As Integer = 0 To cnt - 1
            If i = T Then
                Return MainJSON.Item("data").Item("map").Item("types")(i)
            End If
        Next
        Return ""
    End Function

    Function GetTypeByResourceID(ByVal I As Integer) As String
        Dim Name As String = ""
        For Each useritem In MainJSON.Item("data").Item("ret").Item("items")
            If useritem.Item("I") = I Then
                For Each item In MainJSON.Item("data").Item("ret").Item("itemInfo")
                    If item.Item("L") = useritem.Item("L") Then
                        Return GetTypeByT(item.Item("T"))
                    End If
                Next
            End If
        Next
        Return ""
    End Function

    Function IsValidConfigType(ByVal Type As String) As Boolean
        Select Case Type
            Case "Weapon_LaserType"
                Return True
            Case "Weapon_HellstormLauncherType"
                Return True
            Case "Extra_ExtraType"
                Return True
            Case "Drone_Design_DroneDesignType"
                Return True
            Case "Weapon_WeaponType"
                Return True
            Case "Drone_DroneType"
                Return True
            Case "Generator_EngineType"
                Return True
            Case "Generator_ShieldType"
                Return True
            Case "Generator_GeneratorType"
                Return True
            Case "Extra_Cpu_CPUType"
                Return True
            Case Else
                Return False
        End Select


        Return False
    End Function

#End Region

    Function ParseExactName(ByVal str As String) As String
        Return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str.Split("_"c).Last().ToLower)
        ' Return .ToUpper
    End Function

    Function ParseListToItemarray(ByVal input As List(Of String)) As String
        Dim lst As New List(Of String)
        Dim sb As New System.Text.StringBuilder
        For Each item In input
            lst.Add(CStr(item))
        Next
        For Each itm In lst
            sb.Append(Chr(34) & itm & Chr(34) & ",")
        Next
        Dim tmp As String = sb.ToString.Remove(sb.ToString.Length - 1, 1)
        tmp = tmp.Remove(0, 1)
        tmp = tmp.Remove(tmp.ToString.Length - 1, 1)
        Return tmp
    End Function

End Class
